package com.ip.getip;

import com.ip.getip.util.FileImg;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author leixin
 * @Date 2019/9/29 上午1:47
 * @Version 1.0
 **/
public class ccc {

    public static void main(String[] args) throws Exception {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        URL realUrl = new URL("http://he2911.com/18H_3237.html");
        // 打开和URL之间的连接
        URLConnection connection = realUrl.openConnection();
        // 设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        connection.setRequestProperty("Cookie", "__cfduid=d16e576b63adefc63693d17d000ea2e491569608935; TSCvalue=gb; UM_distinctid=16d73fbafe4433-086244c74ee8988-30624645-144000-16d73fbafe5489; CNZZDATA1273435591=1543777747-1569608637-http%253A%252F%252Fhe2911.com%252F%7C1569608637; CNZZDATA1273380027=441097036-1569603546-http%253A%252F%252Fhe2911.com%252F%7C1569608946; PHPSESSID=ojho4ck81i9bio6m93stja2981");
        // 建立实际的连接
        connection.connect();
        // 获取所有响应头字段
        Map<String, List<String>> map = connection.getHeaderFields();
        // 遍历所有的响应头字段
        for (String key : map.keySet()) {
            System.out.println(key + "--->" + map.get(key));
        }
        // 定义 BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(
                connection.getInputStream(), "UTF-8"));
        String line;
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        String[] split = result.toString().split("Large_cgurl = new Array\\(\\)\\;");
        String[] split2 = split[1].toString().split("i=0\\;");
        String trim = split2[0].trim();
        List list = new ArrayList();
        String[] text = trim.split("\\;");
        for (String s : text) {
            list.add(s.split("\"")[1]);
        }
        System.out.println(list.toString());
        FileImg fileImg = new FileImg();
        String path = list.get(5).toString().substring(list.get(5).toString().length() - 7, list.get(5).toString().length());
        fileImg.downloadPicture(list.get(5).toString(), "//home//leixin//Pictures//" + path);
    }
}

